﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows.Forms;
using static System.Net.Mime.MediaTypeNames;

#region 文件信息
/****************************************************************
*	公司名称：福建力和行智能科技有限公司
*   作    者：Jeff
*   CLR 版本：4.0.30319.42000
*   创建时间：1/1/2025 12:14:27 PM
*	用 户 名：zheqichen 
*   描述说明：
*
*   修改历史：
*		1）	修 改 人：
*			修改日期：
*			修改内容：			
*
*****************************************************************/
#endregion


namespace Lhxzn.LangExtractor
{
    /// <summary>
    /// 类    名:UcTextEdit
    /// 描    述:
    /// 修改时间:1/1/2025 12:14:27 PM
    /// </summary>
    public partial class UcTextEdit : UserControl
    {

        #region 成员变量

        #region private



        #endregion


        #region protected



        #endregion


        #region public
        public LngFileManager LngFileManager { get; set; }

        public RichTextBox RichTextBox { get => rtxt_Main; }
        #endregion

        #endregion


        #region 构造函数
        /// <summary>
        /// 函 数 名:构造函数
        /// 函数描述:默认构造函数
        /// 修改时间:1/1/2025 12:14:27 PM
        /// </summary>
        public UcTextEdit()
        {
            InitializeComponent();
            SetEvents();
            InitializeUI();
        }

        public UcTextEdit(LngFileManager manager):this()
        {
            LngFileManager = manager;
        }
        #endregion

        #region 父类函数重载、接口实现

        #endregion

        #region 函数

        #region private



        #endregion


        #region protected


        protected void SetEvents()
        {
            tsbtn_Edit.Click += OnClickTsbtnEdit;
            tsbtn_Save.Click += OnClickTsbtnSave;
            tsbtn_Undo.Click += OnClickTsbtnUndo;
            tsbtn_Redo.Click += OnClickTsbtnRedo;
        }

        private void OnClickTsbtnUndo(object? sender, EventArgs e)
        {
            rtxt_Main.Undo();
        }

        private void OnClickTsbtnRedo(object? sender, EventArgs e)
        {
            rtxt_Main.Redo();
        }

        private void OnClickTsbtnSave(object? sender, EventArgs e)
        {
            tsbtn_Save.Enabled=!tsbtn_Save.Enabled;
            tsbtn_Edit.Enabled=!tsbtn_Save.Enabled;
            rtxt_Main.Enabled =!tsbtn_Edit.Enabled;
            SaveTextToLngFile();
            rtxt_Main.Text=LngFileManager.GetSelectedText();
            RefreshAnnotations();
        }

        private void OnClickTsbtnEdit(object? sender, EventArgs e)
        {
            tsbtn_Edit.Enabled = !tsbtn_Edit.Enabled;
            tsbtn_Save.Enabled = !tsbtn_Edit.Enabled;
            rtxt_Main.Enabled = !tsbtn_Edit.Enabled;
        }


        #endregion


        #region public
        /// <summary>
        /// 刷新标注
        /// </summary>
        public void RefreshAnnotations()
        {
            // 清除所有现有格式
            rtxt_Main.SelectAll();
            rtxt_Main.SelectionBackColor = rtxt_Main.BackColor;
            rtxt_Main.DeselectAll();
            List<string> annotations = LngFileManager.AnnotationManager.GetAnnotations(LngFileManager.SelectedLngFileName);
            foreach (string annotation in annotations)
            {
                HighlightAnnotation(annotation, Color.Yellow);
            };
        }

        /// <summary>
        /// 保存文本到语言文件
        /// </summary>
        public void SaveTextToLngFile()
        {
            LngFileManager.SaveEditingText(rtxt_Main.Text);
            LngFileManager.ContextSearcher.InitSearching();
        }

        /// <summary>
        /// 初始化UI
        /// </summary>
        public void InitializeUI()
        {
            tsbtn_Edit.Enabled = true;
            this.tsbtn_Save.Enabled = false;
            rtxt_Main.Text = "";
            rtxt_Main.Enabled = false;
        }

        /// <summary>
        /// 高亮标注
        /// </summary>
        /// <param name="textToHighlight"></param>
        /// <param name="highlightColor"></param>
        public void HighlightAnnotation(string textToHighlight, Color highlightColor)
        {
            int originalSelectionStart = rtxt_Main.SelectionStart;
            int originalSelectionLength = rtxt_Main.SelectionLength;
            int startIndex = 0;
            textToHighlight = Regex.Replace(textToHighlight, @"\r?", "");
            while ((startIndex = rtxt_Main.Text.IndexOf(textToHighlight, startIndex)) != -1)
            {
                rtxt_Main.Select(startIndex, textToHighlight.Length);
                rtxt_Main.SelectionBackColor = highlightColor;
                startIndex += textToHighlight.Length;
            }
            rtxt_Main.Select(originalSelectionStart, originalSelectionLength);
        }

        /// <summary>
        /// 高亮文本
        /// </summary>
        /// <param name="text"></param>
        /// <param name="startIndex"></param>
        /// <param name="highlightColor"></param>
        public void HighlightText(string text, int startIndex,Color highlightColor)
        {
            RefreshAnnotations();
            int originalSelectionStart = rtxt_Main.SelectionStart;
            int originalSelectionLength = rtxt_Main.SelectionLength;
            rtxt_Main.Select(startIndex, text.Length);
            rtxt_Main.SelectionBackColor = highlightColor;
            rtxt_Main.ScrollToCaret();
            rtxt_Main.Refresh();
           // rtxt_Main.Select(originalSelectionStart, originalSelectionLength);
            rtxt_Main.ResumeLayout();

        }

        /// <summary>
        /// 删除多个标注
        /// </summary>
        /// <param name="text"></param>
        public void RemoveAnnotations(string text)
        {
            List<string> strs=text.Split('\n').ToList();
            LngFileManager.AnnotationManager.DeleteAnnotations(LngFileManager.SelectedLngFileName, LngFileManager.SelectedSectionName, strs);
        }

        /// <summary>
        /// 添加多个标注
        /// </summary>
        /// <param name="text"></param>
        public void AddAnnotations(string text)
        {
            List<string> strs = text.Split('\n').ToList();
            LngFileManager.AnnotationManager.AddAnnotations(LngFileManager.SelectedLngFileName, strs);
        }

        /// <summary>
        /// 添加选择行的标注
        /// </summary>
        public void AddSelectedAnnotations()
        {
            if (rtxt_Main.SelectionLength == 0)
            {
                return;
            }
            Tuple<string, int, int> selectedLineInfo = GetSelectedLineInfo();
            rtxt_Main.SelectionStart = selectedLineInfo.Item2;
            rtxt_Main.SelectionLength = selectedLineInfo.Item3 - selectedLineInfo.Item2;
            rtxt_Main.SelectionBackColor = Color.Yellow;
            AddAnnotations(selectedLineInfo.Item1);
        }

        /// <summary>
        /// 删除选择行的标注
        /// </summary>
        public void RemoveSelectedAnnotations()
        {
            if(rtxt_Main.SelectionLength == 0)
            {
                return;
            }
            Tuple<string, int, int> selectedLineInfo = GetSelectedLineInfo();
            rtxt_Main.SelectionStart = selectedLineInfo.Item2;
            rtxt_Main.SelectionLength = selectedLineInfo.Item3- selectedLineInfo.Item2;
            rtxt_Main.SelectionBackColor = rtxt_Main.BackColor;
            RemoveAnnotations(selectedLineInfo.Item1);
        }

        /// <summary>
        /// 获取选择的行信息
        /// </summary>
        /// <returns></returns>
        public Tuple<string,int,int> GetSelectedLineInfo()
        {
            string allText = rtxt_Main.Text;
            int selectionStart = rtxt_Main.SelectionStart;
            int selectionEnd = selectionStart + rtxt_Main.SelectionLength;
            int startLineIndex = 0;
            for (int i = 0; i < selectionStart; i++)
            {
                if (allText[i] == '\n')
                {
                    startLineIndex = i + 1;
                }
            }
            int endLineIndex = allText.LastIndexOf('\n', selectionEnd - 1);
            if (endLineIndex == -1)
            {
                endLineIndex = allText.Length; 
            }
            else
            {
                endLineIndex = endLineIndex== selectionEnd-1? endLineIndex: allText.IndexOf('\n', endLineIndex + 1);
                if (endLineIndex == -1)
                {
                    endLineIndex = allText.Length; 
                }
            }
            string selectedRangeText = allText.Substring(startLineIndex, endLineIndex - startLineIndex);
            return new Tuple<string, int, int> (selectedRangeText, startLineIndex, endLineIndex);
        }

        #endregion

        #endregion

    }
}
